/* 
 * File:   main.c
 * Author: tasx
 *
 * Created on 25 settembre 2011, 18.34
 */

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_ttf.h>

#include "util/type.h"
#include "util/settings.h"
#include "gui/gui_background.h"
#include "gui/gui.h"
#include "gui/gui_textbox.h"
#include "gui/gui_label.h"
#include "gui/gui_button.h"
#include "gui/gui_notebook.h"

#include "game/login.h"
#include "util/SDL_net.h"

main_obj *mobj;

gboolean test_button_click() {
    printf("click\n");
    return TRUE;
}

/**
 * inizializza il gioco
 * */
void init() {
    // initialize SDL video
    g_debug("Inizializzo le SDL...");
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("Unable to init SDL: %s\n", SDL_GetError());
        //return 1;
    }
    
    g_debug("Inizializzo le SDLNet...");
    if (SDLNet_Init() < 0)
    {
        printf("Unable to init SDL_Net %s\n", SDL_GetError());
    }
    atexit(SDL_Quit);
    g_debug("Iniziazione SDL completata.");

    // make sure SDL cleans up before exit

    /* alloco la memoria per la struttura principale */
    mobj = g_malloc0(sizeof (main_obj));
    g_debug("sizeof mobj => %d\n", sizeof (main_obj));

    mobj->s = settings_get_default_settings();
    mobj->gm = gui_init_gui();



    g_debug("Inizializzo lo schermo...");
    mobj->main_screen = SDL_SetVideoMode(mobj->s->vs->screen_width, mobj->s->vs->screen_height, mobj->s->vs->screen_bbp, mobj->s->vs->screen_flags);

    g_debug("\tInizializzo il background...");
    //mobj->main_background = gui_background_get_background(mobj);
    //mobj->bs = g_malloc0(sizeof (back_settings));
    mobj->bs = gui_background_init_background(mobj);
    /* TODO eliminare la seguente riga */

    g_debug("\tbackground inizializzato.");

    if (!mobj->main_screen) {
        printf("Non posso creare il video!\n");
        //exit(1);
    }
    g_debug("Schermo inizializzato.");

    g_debug("Inizializzo le strtture del main_obj...");
    mobj->tu = g_malloc0(sizeof (time_update));
    mobj->tu->prev_time_updated = SDL_GetTicks();
    mobj->tu->time_updated = mobj->tu->prev_time_updated;

    mobj->e = (event*) g_malloc0(sizeof (event));
    mobj->e->me = (mouse_event*) g_malloc0(sizeof (mouse_event));

    mobj->gm->elements = NULL;

    g_debug("Inizializzazione main_obj completata.");

    g_debug("Inizializzo TTF...");
    /* inizializzo i font */
    if (TTF_Init() != 0) {
        printf("Impossibilire inizializzare TTF!!\n");
        exit(1);
    }

    mobj->gm->current_font = TTF_OpenFont("res/font/Adamina-Regular.ttf", 20);
    if (mobj->gm->current_font == NULL) {
        printf("Impossibile caricare il font...\n");
        exit(1);
    }
    g_debug("Inizializzazione ttf completate!");


    mobj->sm = g_malloc0(sizeof (string_manager));
    mobj->sm->gt = g_hash_table_new((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal);
    g_hash_table_insert(mobj->sm->gt, "space", " ");
    //textbox *t = gui_textbox_new_empty(mobj);

    //gui_add_element(mobj, (gpointer) t);

    g_debug("Inizializzo fps manager...");
    mobj->fm = g_malloc0(sizeof (fps_manager));
    SDL_initFramerate(mobj->fm);
    SDL_setFramerate(mobj->fm, mobj->s->vs->fps);

    g_debug("end of init\n\n");

    /*
     *
     * TEST DELLA GUI
     * 
     */
    /*
    g_debug("Inizio il test della GUI.");

    textbox *t = gui_textbox_new_empty(mobj);
    t->ge.posx = 20;
    t->ge.posy = 20;
    gui_add_element(mobj, (gpointer) t);
    label *l = gui_label_new_label(mobj, "Ciao mondo!!");
    gui_util_set_element_pos((graphic_element*) l, 20, 150);
    gui_add_element(mobj, (gpointer) l);
    gui_util_set_position_on_screen(mobj, (graphic_element*) l, GUI_POSITION_HMIDDLE);
    gui_util_set_position_on_screen(mobj, (graphic_element*) l, GUI_POSITION_VMIDDLE);
    button *b = gui_button_new(mobj, "Click");
    gui_util_set_position_on_screen(mobj, (graphic_element*) b, GUI_POSITION_LEFT);
    gui_util_set_position_on_screen(mobj, (graphic_element*) b, GUI_POSITION_BOTTOM);
    gui_add_element(mobj, (gpointer) b);
    */

    login_build_login_interface(mobj);
    //gui_add_element(mobj, b1);
}

void main_loop() {
    // program main loop
    SDL_Rect back_rect;

    gboolean done = FALSE;
    while (!done) {
        // message processing loop
        SDL_Event event;
        while (SDL_PollEvent(&event)) {
            // check for messages
            switch (event.type) {
                    // exit if the window is closed
                case SDL_QUIT:
                    done = TRUE;
                    break;

                    // check for keypresses
                case SDL_KEYDOWN:
                {
                    // exit if ESCAPE is pressed
                    if (event.key.keysym.sym == SDLK_ESCAPE)
                        done = TRUE;
                    //g_debug("char: %s", SDL_GetKeyName(event.key.keysym.sym));
                    break;
                }
                case SDL_MOUSEBUTTONDOWN:
                {
                }
            } // end switch

            mobj->event = &event;
            gui_manager_update(mobj);
        } // end of message processing



        mobj->tu->time_updated = SDL_GetTicks();

        back_rect.x = (int) mobj->bs->posx;
        back_rect.y = (int) mobj->bs->posy;
        gui_background_update(mobj);
        SDL_BlitSurface(mobj->main_background, 0, mobj->main_screen, &back_rect);

        gui_manager_update_graphics(mobj);

        SDL_UpdateRect(mobj->main_screen, 0, 0, 0, 0);
        SDL_Flip(mobj->main_screen);

        /* fps control */
        SDL_framerateDelay(mobj->fm);
    } // end main loop
}

/*
 * 
 */
int main(int argc, char** argv) {

    /* carico le principali risorse */
    g_debug("init...\n");
    init();

    g_debug("main loop....\n");
    /* il main loop */
    main_loop();

    return (EXIT_SUCCESS);
}

